home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Japanese / cpsimage.cab / data / xipScripts / nImagesPerPg.elf < prev    next >
Text File  |  2009-04-23  |  9KB  |  260 lines

  1. /*
  2. ** Copyright(c) 2006 Xerox Corp. All Rights Reserved. Copyright protection
  3. ** claimed includes all forms and mattersof copyrightable material and
  4. ** information now allowed by statutory or judicial law or hereinafter granted,
  5. ** including without limitation, material generated from the software programs
  6. ** which are displayed on the screen such as icons, screen display looks, etc.
  7. */
  8. // M.Campanelli  4/2006
  9.  
  10. /* @nImagesPerPg
  11.   // DESCRIPTION
  12.      Takes a directory of images and assembles them into pages with N images
  13.   per page.  If the images are jpeg, image preservation is maintained, i.e.,
  14.   the data is not uncompressed. Otherwise the data is uncompressed when read
  15.   and jpeg compressed when written to file.  The output file format is PDF.
  16.   Usage and import options are listed below:
  17.  
  18.   Usage: xipe thisScript -im  dir:s dirname      // required
  19.                               ipp:i imagesPerPg  // def: 4
  20.                               pgw:d pageWidth    // def: 8.5
  21.                               pgh:d pageHeight   // def: 11.0
  22.                            format:s format       // def: PDF
  23.                               aie:b 0|1          // def: FALSE
  24.                             frame:b 0|1          // def: FALSE
  25.                            shadow:b 0|1          // def: FALSE
  26.                             label:b 0|1          // def: FALSE
  27.                           outfile:s outfilename  // def: dirname.pdf
  28. */
  29.  
  30. IMPORT STRING  dir;
  31. IMPORT INTEGER ipp = 4;        // Number of images per page
  32. IMPORT DOUBLE  pgw = 8.5;      // Page width in inches
  33. IMPORT DOUBLE  pgh = 11.0;     // Page height in inches
  34. IMPORT STRING  outfile;        // based on input directory name
  35. IMPORT DOUBLE  resx = 300;
  36. IMPORT DOUBLE  resy = 300;
  37. IMPORT STRING  format = "pdf"; // output format
  38. IMPORT BOOLEAN frame = FALSE;
  39. IMPORT BOOLEAN shadow = FALSE;
  40. IMPORT BOOLEAN aie = FALSE;
  41. IMPORT BOOLEAN label = FALSE;
  42.  
  43. STRING USAGE = "\
  44.  Takes a directory of images and assembles them into pages with N images\
  45.  per page.  If the images are jpeg, image preservation is maintained, i.e.,\
  46.  the data is not uncompressed. Otherwise the data is uncompressed when read\
  47.  and jpeg compressed when written to file.  The output file is PDF format.\
  48.  Usage and import options are listed below:\n\n\
  49.  Usage: xipe thisScript -im  dir:s dirname      // required\
  50.                              ipp:i imagesPerPg  // def: 4 up\
  51.                              pgw:d pageWidth    // def: "+pgw+" inches\
  52.                              pgh:d pageHeight   // def: "+pgh+" inches\
  53.                           format:s format       // def: "+format+"\
  54.                              aie:b 0|1          // def: "+aie+"\
  55.                            frame:b 0|1          // def: "+frame+"\
  56.                           shadow:b 0|1          // def: "+shadow+"\
  57.                            label:b 0|1          // def: "+label+"\
  58.                          outfile:s outfilename  // def: dirname.pdf\n";
  59.  
  60. // 
  61. // Calculate the center x, y position and max w,h of the image cell for the
  62. // desired number of images per page. Assumes a border of .5 inches at 300spi
  63. // 
  64. PROCEDURE getParams (INTEGER nup, INTEGER count, DOUBLE pgw, DOUBLE pgh) 
  65.   RETURNS (DOUBLE x, DOUBLE y, INTEGER maxw, INTEGER maxh)
  66. {
  67.   DOUBLE nUP = nup;               // Need as a double for calculations
  68.   INTEGER xgrid = nUP.sqrt();     // xgrid, ygrid calculation
  69.   INTEGER ygrid = (nup + xgrid - 1) / xgrid;
  70.   DOUBLE  dxgrid;                 // Need double for calculations
  71.   DOUBLE  dygrid;                 // Need double for calculations
  72.  
  73.   if ( pgw > pgh ) {
  74.     ygrid = xgrid;
  75.     xgrid = (nup + xgrid - 1) / xgrid;
  76.     }
  77.   dxgrid = xgrid;
  78.   dygrid = ygrid;
  79.  
  80.   // Set border offset and take away border from page for calculations
  81.   // Uses 90% of page for images, border is 5%
  82.   INTEGER xoff = .05 * pgw;
  83.   INTEGER yoff = .05 * pgh;
  84.   pgw = .9 * pgw;
  85.   pgh = .9 * pgh;
  86.  
  87.   // Base calculation for center of image of upper left cell
  88.   x = pgw / (2 * xgrid);
  89.   y = pgh / (2 * ygrid);
  90.  
  91.   // Base calculation for max width, height of image cell
  92.   // As more images are added the spacing between images shrinks
  93.   maxw = (1 - dxgrid * (.01/dxgrid)) * pgw / xgrid;
  94.   maxh = (1 - dygrid * (.01/dygrid)) * pgh / ygrid;
  95.  
  96.   // Adjust x,y based on nUP position, i.e., what grid does it live in
  97.   INTEGER xgridpos = count % xgrid + 1;
  98.   INTEGER ygridpos = (count / xgrid) + 1;
  99.  
  100.   x = 2 * (xgridpos -1) * x + x;
  101.   y = 2 * (ygridpos -1) * y + y;
  102.  
  103.   // Finally, add in border offset
  104.   x = x + xoff;
  105.   y = y + yoff;
  106. }
  107.  
  108.  
  109.  
  110. // Load image processing methods
  111. LoadClasses ( filename: "xeng" );
  112.  
  113. // Load Document object
  114. #import "documentio.ucm";
  115. #load "sys/stdlib.elf";
  116. #load "xipProcs/FrameIt.proc";
  117.  
  118.  
  119. // Do we have a directory name
  120. if ( ! dir)
  121.   { print USAGE; return; }
  122.  
  123. // Is the directory real
  124. FILE ddir = new (FILE, path: dir);
  125. if ( !ddir.isDirectory() )
  126.   { SetStatus (op: "Stop", msg: "Invalid or unreadable directory: " + dir + "\n"); return; }
  127.  
  128. // Local variables
  129. STRING file;
  130. XIPIMAGE tmp;
  131. DOUBLE ratio, w,h, cxpos, cypos;
  132. INTEGER delw, delh, i, pgnum;
  133.  
  134. // Get list of files and default sort in ascending order
  135. LIST filelist = ddir.list();
  136. filelist.sort();
  137.  
  138. // create output DOCUMENTWRITER
  139. print "creating Document writer";
  140. DOCUMENTWRITER docwrite;
  141. STATUS status;
  142. if ( !outfile )
  143.   outfile = dir.name() + "." + format;
  144.  
  145. // try-catch to handle filenames with spaces (i.e., on Windows)
  146. try {
  147.    docwrite = CreateDocumentWriter (filename: outfile);
  148. } catch {
  149.    if ( outfile.match(str: " ") )
  150.       docwrite = CreateDocumentWriter (filename: "\""+outfile+"\"");
  151.    if( !docwrite ) {
  152.       print "CreateDocumentWriter failed on second attempt.";
  153.       print status.message;  return;
  154.       }
  155. }
  156.  
  157. // loop through file list
  158. INTEGER j;
  159. DOUBLE  r1, r2;
  160. XIPIMAGE pgimg;
  161. for (j=0; filelist[j]; ) {
  162.  
  163.   for (i=0; filelist[j] && i<ipp; i++) {
  164.     // Get the image uncompressed
  165.     file = filelist[j];
  166.     print "reading file " + dir + "/" + file;
  167.     try {
  168.       tmp = readjpg (filename: dir + "/" + file, compressed: 1).exec();
  169.     } catch {
  170.       try {
  171.         tmp = readimage (filename: dir + "/" + file).exec();
  172.       } catch {
  173.         print "  Above file not an image";
  174.         j++;
  175.         i--;
  176.         continue;
  177.       }
  178.     }
  179.  
  180.     if (aie || frame || shadow || label)
  181.       tmp = tmp.unCompress();
  182.  
  183.     if (aie)
  184.       tmp = tmp.aie ();  // aie before any other operations
  185.     if (frame)
  186.       tmp = FrameIt (size: 10, inImg: tmp );
  187.     if (shadow)
  188.       tmp = tmp.shadow ();
  189.     if (label)
  190.       tmp = tmp.label(string: file.basename(),           // label the image name
  191.                      opacity: .5,
  192.                       center:TRUE,
  193.                     bgcolors: (200,200,200),
  194.                    fposition: (.5, .9),
  195.                     fontsize:24);
  196.  
  197.     // Catch the current set of resolutions
  198.     r1 = tmp.getMember(member:"xres");
  199.     r2 = tmp.getMember(member:"yres");
  200.  
  201.     // Get the center x,y and largest w,h of the image based on position
  202.     getParams (nup:ipp, count: i, pgw: pgw * r1, pgh: pgh * r2)
  203.       Returns ( x: cxpos, y: cypos, maxw: w, maxh: h);
  204.  
  205.     // figure out what the real size should be set at
  206.     ratio = tmp.getMember(member:"height") / tmp.getMember(member:"width");
  207.  
  208.     if ( h/w > ratio) {
  209.       tmp.setMember (member: "width",  value: resx/r1 * w);
  210.       tmp.setMember (member: "height", value: resy/r2 * ratio * w);
  211.       }
  212.     else {
  213.       tmp.setMember (member: "width",  value: resx/r1 * h / ratio);
  214.       tmp.setMember (member: "height", value: resy/r2 * h);
  215.       }
  216.  
  217.     // Set position based on actual width and height
  218.     tmp.setMember (member: "xpos",
  219.                     value: (resx/r1) * (cxpos - (tmp.getMember(member:"width") / 2.0 * r1/resx)) );
  220.     tmp.setMember (member: "ypos",
  221.                     value: (resy/r2) * (cypos - (tmp.getMember(member:"height") / 2.0 * r2/resy)) );
  222.  
  223.  
  224.     // Add ColorRectangle becasue it goes now before the layer
  225.     /* Add Frame, doesn't work correctly
  226.     pgimg.addLayer (image: new (XIPLAYER,
  227.                                 layerType: XIP_ColorRectangle,
  228.                                     width: tmp.getMember (member:"width")+10,
  229.                                    height: tmp.getMember (member:"height")+10,
  230.                                      xpos: tmp.getMember (member: "xpos")-5,
  231.                                      ypos: tmp.getMember (member: "ypos")-5,
  232.                                      xres: tmp.getMember (member: "xres"),
  233.                                      yres: tmp.getMember (member: "yres"),
  234.                                     color: new (XIPCOLOR, 
  235.                                             photometry: XIP_SRGB_COLOR,
  236.                                                      c: (1,1,200) ) ) );
  237.     */
  238.  
  239.     // Add the image to the page
  240.     pgimg.addLayer (image: tmp);
  241.  
  242.     // Get the next file
  243.     j++;
  244.     }
  245.  
  246.   // Set overall page attributes assuming page res based on image res
  247.   pgimg.resolution = (resx,resy);
  248.   pgimg.imageWidth = pgw * pgimg.resolution[0];
  249.   pgimg.imageHeight = pgh * pgimg.resolution[1];
  250.  
  251.   // Add page to the DOCUMENT
  252.   print docwrite.appendPage (pgImg: pgimg);
  253.   pgnum++;
  254.  
  255.   pgimg = new (XIPIMAGE); // Needs to reset to not add all layers to one image
  256.   }
  257.  
  258.  
  259. // We're done
  260.